﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;
using System.Collections;

namespace Yuanlin.Business
{
    public class NewsCategoryBreadCrumb : Literal
    {
        private Queue crumbs = new Queue();
        private HtmlAnchor anchor = null;
        private int upNewsCategoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.UpNewsCategoryId);

        protected override void CreateChildControls()
        {
            crumbs.Enqueue(GetAnchor("资讯类别列表首页", SiteUrls.Instance().GetNewsCategoryListUrl(0)));

            NewsCategory c1 = null;
            NewsCategory c2 = null;

            c1 = BusinessManager.GetNewsCategory(upNewsCategoryId);
            if (c1 == null)
                return;

            c2 = BusinessManager.GetNewsCategory(c1.UpNewsCategoryId.Value);
            if (c2 == null)
            {
                crumbs.Enqueue(GetAnchor(c1.Title.Value, SiteUrls.Instance().GetNewsCategoryListUrl(c1.EntityId.Value)));
                return;
            }

            crumbs.Enqueue(GetAnchor(c2.Title.Value, SiteUrls.Instance().GetNewsCategoryListUrl(c2.EntityId.Value)));
            crumbs.Enqueue(GetAnchor(c1.Title.Value, SiteUrls.Instance().GetNewsCategoryListUrl(c1.EntityId.Value)));
        }

        protected Control GetAnchor(string innerText, string href)
        {
            anchor = new HtmlAnchor();
            anchor.InnerHtml = innerText;
            anchor.Attributes["class"] = "crumbAnchor";
            anchor.HRef = href;

            return anchor;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("资讯类别导航:&nbsp;&nbsp;");

            while (crumbs.Count > 0)
            {
                HtmlAnchor a = (HtmlAnchor)crumbs.Dequeue();

                a.Title = a.InnerHtml;

                if ((crumbs.Count > 0) && (a.InnerHtml.Length > 30))
                {
                    a.InnerHtml = a.InnerHtml.Substring(0, 30) + "...";
                }
                a.RenderControl(writer);

                if (crumbs.Count > 0)
                {
                    writer.Write(" &#187; ");
                }
            }

        }

    }
}
